import { defineStore } from "pinia";
import { ref,computed } from "vue";
import { useTask } from "@/utils/message";
import { useMemberStore } from "@/store/module/member";
import { cartListApi, addCartApi, setCartNumApi, setCartCheckApi, delCartApi } from '@/api/goods';

export const useCartStore = defineStore("cart", () => {
  const cartList = ref<any[]>([]); // 购物车列表
  const isAll = ref<boolean>(false); // 是否全选
  const isUpdate = ref<boolean>(false); // 是否正在编辑中

    // 计算购物车选中数量
  const cartCheckNum = computed(() => {
    let result:number = 0;
    cartList.value.forEach((company: any) => {
      company.list.forEach((sku: any) => {
        if (sku.checked == 1) {
          result += sku.num;
        }
      })
    })
    return result;
  })

  // 计算购物车选中商品总价格
  const cartCheckMoney = computed(() => {
    let result: number = 0;
    cartList.value.forEach((company: any) => {
      company.list.forEach((sku: any) => {
        if (sku.checked == 1) {
          result += (parseFloat(sku.price) * sku.num);
        }
      })
    })
    return result.toFixed(2);
  })

  // 计算购物车商品总数量
  const cartNum = computed(() => {
    let result:number = 0;
    cartList.value.forEach((company: any) => {
      company.list.forEach((sku: any) => {
        result += sku.num;
      })
    })
    return result;
  })

  // 计算已选择的商品
  const selectSku = computed(() => {
    let skuArr: any[] = [];
    cartList.value.forEach((company: any) => {
      company.list.forEach((sku: any) => {
         if (sku.checked == 1) {
           skuArr.push(sku);
        }
      })
    })
    return skuArr;
  })


  // 购物车列表
  const getCartList = async () => {
    const memberStore = useMemberStore();
    // 没有登录，返回空数组
    if (!memberStore.userInfo.userid) return;
    const res: AnyObject = await cartListApi({
      userid: memberStore.userInfo.userid
    })
    if (res.code == 200) {
      cartList.value = formatCartList(res.data);
    } else {
      useTask(res.msg);
    }
  }

  // 购物车列表格式整理
  const formatCartList = (list: any) => {
    let result: any[] = list;
    let companyCheck = true
    let all: boolean = true;
    result.forEach((company: any) => {
      company.list.forEach((sku: any) => {
        if (sku.checked == 1) {
          sku.isCheck = true;
          companyCheck = true;
        } else {
          sku.isCheck = false;
          all = false;
          companyCheck = false;
        }
      })
      company.checked = companyCheck ? true : false;
    })
    isAll.value = all;
    console.log(isAll.value);
    return result;
  }

  // 添加到购物车
  const addCart = async (sku: AnyObject) => {
    const res: AnyObject = await addCartApi(sku);
    if (res.code == 200) {
      useTask("加入购物车成功");
    } else {
      useTask(res.msg);
    }
  };

  // 设置购物车sku数量
  const setCartNum = async (val: AnyObject) => {
    const res: AnyObject = await setCartNumApi(val);
    if (res.code == 200) {
      getCartList();
    } else {
      useTask(res.msg);
    }
  }


  // 选择商品
  const selectProduct = async (selectId: any, is_check:boolean, refresh?:boolean) => {
    const res: AnyObject = await setCartCheckApi({
      id: typeof selectId == "number" ? selectId : selectId.join(","),
      is_check: is_check,
    })
    if (res.code != 200) {
      useTask(res.msg);
    } else {
      if (refresh) {
        getCartList();
      }
    }
  }

  // 编辑
  const update = () => {
    isUpdate.value = !isUpdate.value;
  }

  // 删除
  const removeSku = async (ids: any) => {
    const res: AnyObject = await delCartApi({
      id: typeof ids === "number" ? ids : ids.join(","),
    })

    if (res.code == 200) {
      getCartList();
      isUpdate.value = false;
    } else {
      useTask(res.msg);
    }
  }

  return {
    cartList,
    getCartList,
    addCart,
    setCartNum,
    selectProduct,
    cartCheckMoney,
    cartCheckNum,
    cartNum,
    selectSku,
    isAll,
    isUpdate,
    update,
    removeSku
  }
})
